/**
 * 
 */
package com.css.crm.utility;

import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.dbunit.dataset.Column;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.ITable;

/**
 * @author AIS
 * 
 */
public class CrmTestUtil {

	/**
	 * transfer between db flat xml to vo object
	 * 
	 * @param dataSet
	 * @param tableName
	 * @param toClass
	 * @return
	 * @throws Exception
	 */
	public static Object convert(IDataSet dataSet, String tableName, Class<?> toClass) throws Exception {
		ITable customerTable = dataSet.getTable(tableName);
		Object object = toClass.newInstance();
		Method[] setMethods = toClass.getMethods();
		if (setMethods == null || setMethods.length < 1) {
			return null;
		}
		int row = customerTable.getRowCount();
		Column[] columns = customerTable.getTableMetaData().getColumns();
		String columnName = null;
		String methodName = null;
		Object attributeValue = null;
		Method method = null;
		for (int i = 0; i < row; i++) {
			for (int j = 0; j < columns.length; j++) {
				columnName = columns[j].getColumnName();
				methodName = "set" + convertColumnToMethod(columnName);
				attributeValue = customerTable.getValue(i, columnName).toString();

				if (StringUtils.isEmpty(attributeValue.toString())) {
					continue;
				}

				method = indexMethod(setMethods, methodName);
				if (method == null) {
					continue;
				}

				if (Date.class.equals(method.getParameterTypes()[0])) {
					SimpleDateFormat sdf = new SimpleDateFormat(DateUtil.DATE_FORMAT_COMMON);
					attributeValue = sdf.parse(attributeValue.toString());

				}

				if (Long.class.equals(method.getParameterTypes()[0])) {
					attributeValue = new Long(attributeValue.toString());

				}
				method.invoke(object, attributeValue);
			}
		}

		return object;
	}

	/**
	 * convert dataset xml to object
	 * 
	 * @param dataSet
	 * @param tableName
	 * @param toClass
	 * @return
	 * @throws Exception
	 */
	public static List convertList(IDataSet dataSet, String tableName, Class<?> toClass) throws Exception {
		ITable customerTable = dataSet.getTable(tableName);
		List<Object> resuList = new ArrayList<Object>();
		Object object = null;
		Method[] setMethods = toClass.getMethods();
		if (setMethods == null || setMethods.length < 1) {
			return null;
		}
		int row = customerTable.getRowCount();
		Column[] columns = customerTable.getTableMetaData().getColumns();
		String columnName = null;
		String methodName = null;
		Object attributeValue = null;
		Method method = null;
		for (int i = 0; i < row; i++) {
			object = toClass.newInstance();
			for (int j = 0; j < columns.length; j++) {
				columnName = columns[j].getColumnName();
				methodName = "set" + convertColumnToMethod(columnName);
				attributeValue = customerTable.getValue(i, columnName).toString();

				if (StringUtils.isEmpty(attributeValue.toString())) {
					continue;
				}

				method = indexMethod(setMethods, methodName);
				if (method == null) {
					continue;
				}

				if (Date.class.equals(method.getParameterTypes()[0])) {
					SimpleDateFormat sdf = new SimpleDateFormat(DateUtil.DATE_FORMAT_COMMON);
					attributeValue = sdf.parse(attributeValue.toString());

				}

				if (Long.class.equals(method.getParameterTypes()[0])) {
					attributeValue = new Long(attributeValue.toString());

				}
				method.invoke(object, attributeValue);
			}

			resuList.add(object);
		}

		return resuList;
	}

	private static Method indexMethod(Method[] arr, String name) {
		for (int i = 0; i < arr.length; i++) {
			if (name.equals(arr[i].getName())) {
				return arr[i];
			}
		}

		return null;
	}

	private static String convertColumnToMethod(String columnName) {
		String attr = "";
		String[] cols = columnName.split("_");
		for (int i = 0; i < cols.length; i++) {
			attr = attr + cols[i].substring(0, 1).toUpperCase() + cols[i].substring(1).toLowerCase();
		}

		return attr;

	}
}
